package com.erastep.base;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import com.erastep.util.CommonMethod;

public class ConnectionFactory {
	/**
	 * 开启数据库连接
	 * */
	public static Connection getDataConn(){
		Connection conn = null; 
		try {
			Class.forName("com.mysql.jdbc.Driver");
		    //String url = "jdbc:mysql://223.6.35.72:3306/zkz0v1_db?user=zkz0v1&password=m6a5g2f7"; 
			String url = "jdbc:mysql://127.0.0.1:3306/erastep?user=root&password=753951";
			try {
				conn = DriverManager.getConnection(url);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} 
            try {
				if(!conn.isClosed()) 
					System.out.println("*************数据库连接成功************!");
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		return conn;
	}
	/**
	 * 关闭数据库连接
	 * */
	public static Boolean closeConn(Connection conn){
		if(conn != null){
			try {
				if(!conn.isClosed()){
					conn.close();
					System.out.println("#############数据库关闭成功#############!");
					return true;
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return false;
	}
	public static void main(String[] args) {
		Connection conn = getDataConn();
		if(conn == null){
			System.out.println("数据库连接失败!");
		}else{
			System.out.println("数据库连接成功!"+CommonMethod.getEncryptPwd("123456"));
			ConnectionFactory.closeConn(conn);
		}
	}
}
